// ============================== 导入依赖 ============================== //

import CORE from '@/CORE'
import Client from '@api/parking/api/ParkRoadwayApi'

import ListClass from '@api/parking/model/RoadwayRoadwayListQueryReq'
import OneClass from '@api/parking/model/RoadwayReq'
import AddClass from '@api/parking/model/RoadwayOperateReq'
import BaseClass from '@api/parking/model/RoadwayDto'
import VideoClass from '@api/parking/model/VideoDeviceDto'
import DeviceClass from '@api/parking/model/BindDeviceDto'

const EditClass = AddClass
const DelClass = OneClass

const CLIENT = CORE.grpcCLIENT(Client)

const VIDEO = [VideoClass, 'videoDevice', 1]
const DEVICE = [DeviceClass, 'bindDevice', 1]
const BASE = [BaseClass, 'roadway', null, (dto, obj) => CORE.grpcDTO(dto, obj, VIDEO, DEVICE)]

// ============================== 导出远程接口 ============================== //

export default {
  // —————————————————————————————— 列表 —————————————————————————————— //

  async getList(param, TIP = '查询车道列表') {
    const DTO = CORE.grpcDTO(ListClass, param, BASE)
    const resp = await CORE.grpc(TIP, CLIENT, 'queryRoadwayList', DTO)
    return [resp.comQuery.totalCount, resp.content || []]
  },

  // —————————————————————————————— 单项 —————————————————————————————— //

  async getOne({ id }, TIP = '查询车道详情') {
    const DTO = CORE.grpcDTO(OneClass, { id: [id] })
    const resp = await CORE.grpc(TIP, CLIENT, 'queryRoadwayDetail', DTO)
    return resp.roadway || {}
  },

  // —————————————————————————————— 增 —————————————————————————————— //

  async add(one, TIP = '新增车道') {
    const DTO = CORE.grpcDTO(AddClass, one, BASE)
    await CORE.grpc(TIP, CLIENT, 'insertRoadway', DTO)
    return CORE.grpcOK(TIP)
  },

  // —————————————————————————————— 改 —————————————————————————————— //

  async edit(one, TIP = '设置车道') {
    const DTO = CORE.grpcDTO(EditClass, one, BASE)
    await CORE.grpc(TIP, CLIENT, 'updateRoadway', DTO)
    return CORE.grpcOK(TIP)
  },

  // —————————————————————————————— 删 —————————————————————————————— //

  async del({ id }, TIP = '删除车道') {
    const DTO = CORE.grpcDTO(DelClass, { id: [id] })
    await CORE.grpc(TIP, CLIENT, 'deleteRoadway', DTO)
    return CORE.grpcOK(TIP)
  },
}
